package kr.co.project1.controller;

import java.util.List;

import kr.co.project1.base.PageDto;
import kr.co.project1.service.BoardService;
import kr.co.project1.vo.BoardVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/board")
public class BoardController {
	
	private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
	
	@Autowired
	private BoardService boardService;
	
	/**
	 * boardCode : 1 ==> 일반 게시판
	 * boardCode : 2 ==> Q&A
	 * boardCode : 3 ==> 답변형 게시판
	 */
	
	@RequestMapping(value = "/list")
	public String list(BoardVO boardVO,Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		PageDto<BoardVO> result = boardService.list(boardVO);
		model.addAttribute("result",result);
		model.addAttribute("boardCode",boardVO.getBoardCode());
		
		return "board/list";
	}
	
	@RequestMapping(value = "/getForm")
	public String getForm(BoardVO boardVO,Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		model.addAttribute("boardCode",boardVO.getBoardCode());
		model.addAttribute("seq",boardVO.getSeq());
		
		return "board/form";
	}
	
	@RequestMapping(value = "/insertItem")
	public String insertItem(BoardVO boardVO,Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		boardService.insertItem(boardVO); 
		
		return list(boardVO, model);
	}
	
	@RequestMapping(value="/getItem")
	public String getItem(BoardVO boardVO,Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		BoardVO result = boardService.getItem(boardVO); 
		String reply = Integer.toString(boardVO.getBoardCode());
		if("3".equals(reply) ){
			List<BoardVO> replyList = boardService.replyList(boardVO);
			model.addAttribute("replyList",replyList);
		}
		model.addAttribute("result",result);

		return getForm(boardVO, model);
	}
	
	@RequestMapping(value="/modifyItem")
	public String modifyItem(BoardVO boardVO,Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		boardService.modifyItem(boardVO); 
		
		return list(boardVO, model);
	}
	
	@RequestMapping(value="/deletes")
	public String deletes(BoardVO boardVO, Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		boardService.deletes(boardVO);
		
		return list(boardVO, model);
	}
	
	@RequestMapping(value="/replyList")
	public String replyList(BoardVO boardVO, Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		List<BoardVO> replyList = boardService.replyList(boardVO);
		model.addAttribute("replyList",replyList);
		
		return "board/replyViewList";
	}
	
	@RequestMapping(value="/replyInsert")
	public String replyInsert(BoardVO boardVO, Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		boardService.replyInsert(boardVO);
		
		return getItem(boardVO, model);
	}
	
	@RequestMapping(value="/replyDelete")
	public String replyDelete(BoardVO boardVO, Model model){
		logger.debug("boardVO Parameter = {}", boardVO);
		
		boardService.replyDelete(boardVO);
		
		return getItem(boardVO, model);
	}
	
	
}
